package com.woniuxy.factory.util;

import com.woniuxy.factory.entity.SystemMenu;

import java.util.List;

/**
 * @NTLX data 2020 08 24
 */
public class NodeUtil {


    public static int[] toNodes(List<SystemMenu> list, int[] nodes, int id) {
        int i = 1;
        nodes[nodes.length-1] = id;
        for (SystemMenu node : list) {
            if (node.getSymId() == id) {
                //递归
                return findParent(list, nodes, node.getPid(), i);
            }
        }
        return nodes;
    }

    private static int[] findParent(List<SystemMenu> list, int[] nodes, Integer pid, int i) {
        i++;
        if (pid != 1) {
            //赋值
            nodes[nodes.length-i] = pid;
            for (SystemMenu node : list) {
                //递归寻找父节点
                if (node.getSymId() == pid)
                    findParent(list, nodes, node.getPid(), i);
            }
        }
        return nodes;
    }
}
